<?

require_once './application/core/CoreBase.php';
require_once './application/core/Client/consoleSoap.php';
require_once './application/models/Tables/cache.php';
require_once './application/entity/console/command.php';
require_once './library/XML/xmlToObjArray.php';

class commandCore extends IndexCore
{

    private $_identity;
    private $_ip;
    private $_model;
    private $_type;
    private $_object;
    private $_cmd;
    private $_obj;
    private $_xml;
    private $_proto;
    private $_data;

    public function __construct(&$controller)
    {
        $this->_controller = $controller;
        $this->_session = new Zend_Session_Namespace('session');
        $this->_session->command = 0;
    }

    public function command()
    {
        $this->_ip = $this->_controller->getRequest()->getParam('ip');
        $this->_model = $this->_controller->getRequest()->getParam('model');
        $this->_type = $this->_controller->getRequest()->getParam('type');
        $this->_identity = $this->_controller->getRequest()->getParam('identity');
        $this->_cmd = $this->_controller->getRequest()->getParam('cmd');
        $this->_proto = $this->_controller->getRequest()->getParam('proto');
        if (stristr($this->_proto, "auto"))
            $this->_proto = "ssh,telnet";
        $this->_obj = new CoreBase($this->_controller);
        if ($this->_identity != '-')
            $this->_identity = $this->_obj->getUsernameBase($this->_identity);
        else
            $this->_identity = '';
        $this->_cmd = base64_encode($this->_cmd);
        $this->_ip = explode(',', $this->_ip);
        foreach ($this->_ip as $ip)
        {
            $this->_session->command++;
            $ip = $this->parseArrayIp($ip);
            $params = array('Ip' => $ip,
                'Service' => 'command',
                'Model' => $this->_model,
                'Type' => $this->_type,
                'Name' => 'command',
                'Identity' => $this->_identity,
                'Protocol' => $this->_proto,
                'Cmd' => $this->_cmd);
            $this->_object = new consoleSoap($this->_controller);
            $this->_object->command($params, 'commandresp');
        }
        $this->MiseEnAttenteReponse(true, '/console/commandresp', $this->_session->command);
    }

    public function commandResp()
    {
        $this->_data = $this->_controller->getRequest()->getParam('donnees');
        $cache = new cache();
        $rst = $cache->getInfo($this->_controller->_idClient, "commandresp");
        if ($rst != false)
        {
            foreach ($rst as $key => $val)
            {
                $this->_data--;
                $this->_obj = new CoreBase($this->_controller);
                $this->_xml = $this->_obj->parseResp($rst[$key]['Xml']);
                if (!empty($this->_session->commandResp))
                    $tmp = $this->_session->commandResp;
                else
                    $tmp = '';
                $tmp .= $this->parseCommandResp($this->_xml) . ';;;';
                $this->_session->commandResp = $tmp;
                $cache->deleteInfo($this->_controller->_idClient, 'commandresp', $rst[$key]['Id']);
            }
            if ($this->_data == 0)
            {
                $this->_obj = new commandData();
                $this->_obj->result = $this->_session->commandResp;
                $this->MiseEnAttenteReponse(false, '/console/commandresp', $this->_obj);
                $this->_session->commandResp = null;
            }
            else
                $this->MiseEnAttenteReponse(true, '/console/commandresp', $this->_data);
        }
        else
            $this->MiseEnAttenteReponse(true, '/console/commandresp', $this->_data);
    }

    private function parseCommandResp($xml)
    {
        $this->_data = base64_decode($xml['COMMAND'][0]['DATA'][0][0]);
        if ($this->_data == '-1')
            $this->_data .= ',;-;,' . $xml['COMMAND'][0]['IP'][0][0];
        return $this->_data;
    }

}